<?php
namespace app\manager\model;

use think\Db;
use think\Model;
use app\common\traits\model\TraitModel;

class Users extends Model
{
	use TraitModel;

	protected $autoWriteTimestamp = 'timestamp';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

	protected $table = 'users';
	const WAIT_CHECK = 0;//待审核
	const CHECKED = 1;//审核通过
	const UNCHECKED = -99;//审核未通过

	public function getStatusAttr($value)
    {
        $status = [0=>'待审核',1=>'审核通过',-99=>'审核未通过'];
        return $status[$value];
    }

    public function getSexAttr($value)
    {
        $status = [0=>'女',1=>'男',2=>'保密'];
        return $status[$value];
    }
    public function getAuthentication($value)
    {
    	if(empty($value)){
    		return '未认证';
    	}
        $status = [0=>'未认证',1=>'已认证'];
        return $status[$value];
    }
    public function camps(){
        return $this->belongsTo('Camps','camp_id');
    }


	public function getUserList($querys = [])
	{
		$id = session('manager.id');
        $camp_id = model('CampManagers')->where('id',$id)->value('camp_id');//halt($camp_id);
		$querys = array_merge($querys,['camp_id' => $camp_id]);
		return $this->getInfoList($querys);
	}

	public function getWaitcheckedUser($status)
	{
		return $this->where('status',$status)->select();
	}

	public function getUserInfo($id)
	{
		return $this->where('id',$id)->find();
	}

	public function setChecked($id,$status)
	{
		$userinfo = $this->where('id',$id)->find();
        $userinfo->status = $status;
        return $userinfo->save();
	}

	public function changeStatus($id ,$status)
	{
			$this->db()->startTrans();
			try{
				$count = model('Users')->setChecked($id,$status);
				$caravan = model('Caravans')->setChecked($id,$status);
				if ($count && $caravan) {
					$reviewer = session('manager.name');
				$userinfo = model('Users')->where('id',$id)->find();
				$userinfo->reviewer = $reviewer;
				$userinfo->save();
				}

				$this->db()->commit();
            	return true;
			} catch (Exception $e) {
	            $this->db()->rollback();
	            return false;
	        }
		

	}

	








}